package com.example.lenovo.myapplication;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import java.util.ArrayList;

class StuAdapter extends RecyclerView.Adapter<StuViewHolder> {
    private Context context;
    private ArrayList<Student> studentArrayList;

    public StuAdapter(Context context, ArrayList<Student> studentArrayList){
        this.context =context;
        this.studentArrayList=studentArrayList;
    }
    @NonNull
    public StuViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.list_item,viewGroup,false);
        StuViewHolder stuViewHolder = new StuViewHolder(view);
        return stuViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull StuViewHolder stuHolder, int i) {
        final Student s = studentArrayList.get(i);

        stuHolder.headView.setImageResource(s.getImgId());
        stuHolder.nameView.setText(s.getName());
        stuHolder.ageView.setText(String.valueOf(s.getAge()));

        stuHolder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(context,s.getName()+" "+s.getAge(),Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public int getItemCount() {
        return studentArrayList.size();

    }
}
